package api.action;

import api.model.ApiAuthInter;
import api.service.ApiAuthInterService;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.system.cache.CacheUtil;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import manage.action.StatusAction;

@ActionMeta(name="api/apiAuthInter")
public class ApiAuthInterAction extends StatusAction {
    private ApiAuthInter model;

    public JSONMessage doSave(){
        JSONMessage result=new JSONMessage();
        try {
            verifyAdminOperPower(getStatusPower());
            String msg=getService(ApiAuthInterService.class).save(model);
            result.push("model", model);
            fillJSONResult(result,true,msg);
        } catch (Exception e) {
            fillJSONResult(result,false,e);
        }
        return result;
    }
    public VueResult toEdit() throws Exception {
        verifyMenu("A2111");
        verifyAdminOperPower(getStatusPower());
        if(null!=model&&!StringUtil.isSpace(model.getName())&&!StringUtil.isSpace(model.getAuth().getName())){
            model=getService(ApiAuthInterService.class).get(model.getAuth().getAuthId(),model.getName());
        }else if(null!=model&& ObjectUtil.notEmptyOid(model.getAuth())){
            model.setType("B");
        }else{
            throw new MException(this.getClass(),"参数错误");
        }
        return new VuePageResult("page/base/api/auth/editApiAuthInter.js")
                .addParam("model",model);
    }
    @RequestMeta(params = {"authOid"})
    public JSONMessage getList(String authOid){
        JSONMessage result=new JSONMessage();
        try {
            verifyAdminOperPower(getStatusPower());
            result.push("list", ModelQueryList.getModelList(ApiAuthInter.class,new String[]{"*","auth.authId"},null,
                    QueryCondition.eq("auth.oid",authOid)));
            fillJSONResult(result,true,"");
        } catch (Exception e) {
            fillJSONResult(result,false,e);
        }
        return result;
    }
    @RequestMeta(params = {"authId","name"})
    public JSONMessage getParamExample(String authId,String name){
        JSONMessage result=new JSONMessage();
        try {
            verifyAdminOperPower(getStatusPower());
            result.push("example",getService(ApiAuthInterService.class).getParamExample(authId,name));
            fillJSONResult(result,true,"");
        } catch (Exception e) {
            fillJSONResult(result,false,e);
        }
        return result;
    }

    @Override
    public void doRecoveryCallback() throws Exception {
        model= ModelQueryList.getModel(model,"auth.authId","name");
        CacheUtil.clear(ApiAuthInter.class,model.getAuth().getAuthId(),model.getName());
    }

    @Override
    public void doDisableCallback() throws Exception {
        model= ModelQueryList.getModel(model,"auth.authId","name");
        CacheUtil.clear(ApiAuthInter.class,model.getAuth().getAuthId(),model.getName());
    }
    @Override
    public ApiAuthInter getModel() {
        return model;
    }

    public void setModel(ApiAuthInter model) {
        this.model = model;
    }

    @Override
    public String[] getStatusPower() {
        return new String[]{"manage_api_power"};
    }
}
